package com.kantboot.functional.pay.wechat.setting;

import cn.hutool.core.bean.BeanUtil;
import com.kantboot.functional.pay.wechat.util.WechatPayPlatformParam;
import com.kantboot.system.setting.service.ISysSettingService;
import com.kantboot.util.common.exception.BaseException;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Component
public class WechatPaySetting {

    @Resource
    private ISysSettingService settingService;

    public WechatPayPlatformParam getWechatPayPlatformParam() {
        HashMap<String, String> wechat = settingService.getMapByGroupCode("wechat");
        return BeanUtil.copyProperties(wechat, WechatPayPlatformParam.class);
    }

    /**
     * 获取支付私钥
     */
    public String getPayPrivateKey() {
        String value = settingService.getValue("wechat", "payPrivateKey");
        if (value == null) {
            throw BaseException.of("payCertKeyIsNull", "支付证书密钥为空");
        }
        // 去除证书头尾
        value = value.replace("-----BEGIN PRIVATE KEY-----", "")
                .replace("-----END PRIVATE KEY-----", "");
        value=value.trim();
        return value;
    }

}
